﻿using Dapper;
using Estsh.Core.Base;
using Estsh.Core.Dapper;
using Estsh.Core.Model.EnumUtil;
using Estsh.Core.Models;
using Estsh.Core.Repositories;
using Estsh.Core.Wms.IRepositories;
using System.Data;
using System.Text;

namespace Estsh.Core.Wms.Repositories
{
    /// <summary>
    /// 拆分台车
    /// </summary>
    public class SplitCarrierRepository : BaseRepository<BaseEntity>, ISplitCarrierRepository
    {
        public SplitCarrierRepository(DapperDbContext _dapperDbContext) : base(_dapperDbContext)
        {

        }
        /// <summary>
        /// 台车信息
        /// </summary>
        /// <param name="locateName"></param>
        /// <returns></returns>
        public List<WmsRackPackage> GetCarrierByName(string rackNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("select * from wms_rack_package (nolock) where rack_no= '" + rackNo + "'");
                List<WmsRackPackage> sysLocates = dbConn.Query<WmsRackPackage>(stringBuilder.ToString()).ToList();
                return sysLocates;
            }
        }

        /// <summary>
        /// 产品条码号查询
        /// </summary>
        /// <param name="locateName"></param>
        /// <returns></returns>
        public List<SysStock> GetSysStockBycartonNo(string cartonNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("select * from sys_stock (nolock) where carton_no= '" + cartonNo + "'");
                List<SysStock> sysLocates = dbConn.Query<SysStock>(stringBuilder.ToString()).ToList();
                return sysLocates;
            }
        }

        /// <summary>
        /// 台车信息根据零件号
        /// </summary>
        /// <param name="rackNo"></param>
        /// <returns></returns>
        public List<WmsRackPackage> GetCarrierByPartNo(string partNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("select * from wms_rack_package (nolock) where corton_no= '" + partNo + "'");
                List<WmsRackPackage> sysLocates = dbConn.Query<WmsRackPackage>(stringBuilder.ToString()).ToList();
                return sysLocates;
            }
        }


        public List<WmsRackPackage> GetCarrierByRackAndPartNo(string rackNo, string partNo)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                StringBuilder stringBuilder = new StringBuilder();
                stringBuilder.Append("select * from wms_rack_package (nolock) where rack_no='" + rackNo + "' and corton_no= '" + partNo + "'");
                List<WmsRackPackage> sysLocates = dbConn.Query<WmsRackPackage>(stringBuilder.ToString()).ToList();
                return sysLocates;
            }
        }

        public List<SysLocate> CheckLocateStatus(string locateName)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                dbConn.Open();
                string result = string.Empty;

                StringBuilder sql = new StringBuilder(1024);
                sql.Append(@"SELECT a.*,B.*,c.erp_warehouse FROM dbo.sys_locate a (nolock) 
                            LEFT JOIN sys_locate_part b (nolock) ON a.locate_id = b.locate_id 
                            LEFT join sys_zone as c (nolock) on a.zone_id=c.zone_id  
                            WHERE a.locate_name =@locateName ");
                DynamicParameters parameters = new DynamicParameters();
                parameters.Add("@locateName", locateName);

                return dbConn.Query<SysLocate>(sql.ToString(), parameters).ToList();
            }
        }
        public int SQLTransaction(List<string> sqlStrings, List<DynamicParameters> parameterList)
        {
            using (IDbConnection dbConn = dapperDbContext.GetDbConnection())
            {
                if (dbConn.State == ConnectionState.Closed)
                {
                    dbConn.Open();
                }
                //执行事务
                IDbTransaction transaction = dbConn.BeginTransaction();
                if (parameterList == null || parameterList.Count == 0)
                {
                    try
                    {
                        for (int i = 0; i < sqlStrings.Count; i++)
                        {
                            dbConn.Execute(sqlStrings[i], null, transaction);
                        }
                        transaction.Commit();
                        return 1;
                    }
                    catch (Exception exception)
                    {
                        transaction.Rollback();
                        return 0;
                    }
                }
                else
                {
                    try
                    {
                        for (int i = 0; i < sqlStrings.Count; i++)
                        {
                            dbConn.Execute(sqlStrings[i], parameterList[i], transaction);
                        }
                        transaction.Commit();
                        return 1;
                    }
                    catch (Exception exception)
                    {
                        transaction.Rollback();
                        return 0;
                    }
                }
            }
        }

    }
}
